//
//  AdvertViewController.m
//  TouTiao
//
//  Created by 柴旭 on 16/9/12.
//  Copyright © 2016年 Match. All rights reserved.
//

#import "AdvertViewController.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import <UIImageView+WebCache.h>
#import "AdvertModel.h"
#import "TouTiaoTabBarController.h"
@interface AdvertViewController ()
@property (weak, nonatomic) IBOutlet UIView *ADView;
@property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)jump:(id)sender;
@property(nonatomic,strong)AdvertModel *model;
@property(nonatomic,strong)UIImageView *imgView ;
@property(nonatomic,strong) NSTimer *timer;
@property(nonatomic,assign)CGFloat h;
@property(nonatomic,assign)CGFloat w;

@end

@implementation AdvertViewController

-(UIImageView*)imgView{
    //按宽高比设置广告尺寸
    CGFloat scrrwnW=self.view.bounds.size.width;
    if (_imgView==nil) {
        
        if (_h) {
            _imgView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, scrrwnW, (_h/_w)*scrrwnW)];
        }else{
            _imgView =[[UIImageView alloc]initWithFrame:self.view.bounds];
        }
    }
    return _imgView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    [self addGesture];
    [self timer];
}

#pragma mark 设置定时器
-(void)timer{
    //设置定时器  不用时一定要销毁
    _timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(second1) userInfo:nil repeats:YES];
    
}
-(void)second1{
    static int second =5;
    NSString *string=[NSString stringWithFormat:@"点击跳转"];
    [_button setTitle:string forState:UIControlStateNormal];
    if (second==0) {
        //当计时器为0时 让tabbarcontroller重新成为根控制器
        //获取窗口 并改变根控制器
        TouTiaoTabBarController *tabBar=[[TouTiaoTabBarController alloc]init];
        [UIApplication sharedApplication].keyWindow.rootViewController=tabBar;
        //销毁定时器
        [_timer invalidate];
    }
    second--;
}
#pragma mark 添加手势
-(void)addGesture{
    
    _imgView.userInteractionEnabled=YES;//允许与用户交互
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
    [_imgView addGestureRecognizer:tap];
}
-(void)click:(UITapGestureRecognizer *)sender{
    //打开应用级别的程序 用 UIAppLication
    NSURL *url=[NSURL URLWithString:_model.rl];
    [[UIApplication sharedApplication]openURL:url];
    
}
#pragma mark 加载数据
-(void)loadData{
    //AFN  使用管理者
    AFHTTPSessionManager * manger=[AFHTTPSessionManager manager];
    //定义数据类型 AFN第三方没有  需手动加
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/x-javascript"];
    //请求数据--->解析数据--->plist文件--->字典转模型--->显示数据
    NSString *url=@"http://mi.gdt.qq.com/gdt_mview.fcg?posw=640&posid=1090214194677244&ext=%7B%22req%22%3A%7B%22c_osver%22%3A%229.3.5%22%2C%22sdk_src%22%3A%22%22%2C%22c_isjailbroken%22%3Afalse%2C%22muidtype%22%3A2%2C%22jsver%22%3A%224.3.000%22%2C%22c_device%22%3A%22iPhone%205s%22%2C%22c_h%22%3A1136%2C%22muid%22%3A%22080ed9f84b8a0be8974b81000124b003%22%2C%22lng%22%3A0%2C%22c_pkgname%22%3A%22com.spriteapp.baisibdj%22%2C%22c_os%22%3A%22ios%22%2C%22scs%22%3A%22000126d70bb2%22%2C%22conn%22%3A1%2C%22c_devicetype%22%3A1%2C%22c_w%22%3A640%2C%22carrier%22%3A1%2C%22c_sdfree%22%3A3038257152%2C%22lat%22%3A0%2C%22c_ori%22%3A0%2C%22c_dpi%22%3A320%2C%22sdkver%22%3A%224.3%22%2C%22deep_link_version%22%3A1%2C%22postype%22%3A4%2C%22tmpallpt%22%3Atrue%2C%22loc_accuracy%22%3A0%7D%7D&count=1&posh=1136&datatype=2&adposcount=1";
    [manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        //NSLog(@"%@",responseObject);
        [responseObject writeToFile:@"/Users/Match.C/Desktop/cxguanggao.plist" atomically:YES];
        //解析数据
        // ADModel *model=[ADModel mj_objectWithKeyValues:responseObject[@"data"]];
        //NSLog(@"%@",model.h);
        NSDictionary *dict=responseObject[@"data"];
        NSDictionary *dict1=dict[@"1090214194677244"];
        NSArray *array=dict1[@"list"];//求图片地址
        //求宽高
        NSDictionary *dict2=dict1[@"cfg"];
        NSDictionary *dict3=dict2[@"playcfg"];
        _model=[AdvertModel advertModelWithDict:dict3];
        if (_model.h) {
            _w=[_model.w doubleValue];
            _h=[_model.h doubleValue];
            
        }
        //字典转模型
        _model=[AdvertModel advertModelWithDict:array[0]];
        //加载图片
        if (_model.img) {
            
            NSURL *url=[NSURL URLWithString:_model.img];
            [self.imgView sd_setImageWithURL:url];
            [self.ADView addSubview:_imgView];
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
- (IBAction)jump:(id)sender {
    TouTiaoTabBarController *tabBar=[[TouTiaoTabBarController alloc]init];
    [UIApplication sharedApplication].keyWindow.rootViewController=tabBar;
    [_timer invalidate];
}

@end
